package com.xtravel.tweet.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import com.lidroid.xutils.BitmapUtils;
import com.xtravel.R;
import com.xtravel.common.adapter.BaseDataAdapter;
import com.xtravel.common.widget.TweetImageView;
import com.xtravel.tweet.activity.ImageDetailActivity;
import com.xtravel.tweet.database.dto.TweetSource;

import java.util.ArrayList;
import java.util.List;

public class ImageListAdapter extends BaseDataAdapter<TweetSource> {
    private final BitmapUtils bitmapUtils;
    private int imgWidth;
    private int tweetPosition;
    private final Context context;
    private LayoutInflater mInflater;

    public ImageListAdapter(Context context, BitmapUtils bitmapUtils, List<TweetSource> data) {
        super(data);
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        this.tweetPosition = tweetPosition;
        this.bitmapUtils = bitmapUtils;

        WindowManager wm = ((Activity) context).getWindowManager();
        DisplayMetrics dm = new DisplayMetrics();
        int width = wm.getDefaultDisplay().getWidth();
        float density = context.getResources().getDisplayMetrics().density;

        imgWidth = (int) (width - ((32) * density)) / 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.image_view_item, null);
            holder.imageView = (TweetImageView) convertView.findViewById(R.id.image);
            // setRancle(convertView);
            setRancle(holder.imageView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        TweetSource image = (TweetSource) getItem(position);

        // DisplayMetrics dm = new DisplayMetrics();
        // ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        // final int slength = (int) (160 * dm.density);

        // holder.imageView.setImageUrl(image.url);

        // 加载网络图片
        holder.imageView.setImageUrl(bitmapUtils, image.url);

        // AsyncBitmapManager.getInstanse().put(tweetPosition, image.url, holder.imageView);

        /*
        LayoutParams params = holder.imageView.getLayoutParams();
        params.width = (int) (75 * dm.density);
        params.height = (int) (75 * dm.density);
        holder.imageView.setLayoutParams(params);
        */

        holder.imageView.setTag(position);
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View imageView) {
                int index = (Integer) imageView.getTag();
                Intent intent = new Intent(context, ImageDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putStringArray("url", getImageUrls());
                bundle.putInt("start", index);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });

        return convertView;
    }

    private void setRancle(View view) {
        LayoutParams lp = view.getLayoutParams();
        if (lp == null) {
            lp = new LayoutParams(imgWidth, imgWidth);
        }

        lp.width = imgWidth;
        lp.height = imgWidth;
        view.setLayoutParams(lp);
    }

    private String[] getImageUrls() {
        ArrayList<String> urls = new ArrayList<String>();
        for (TweetSource s : dataList) {
            urls.add(s.largeUrl);
        }

        String[] sa = new String[urls.size()];
        return urls.toArray(sa);
    }

    public final class ViewHolder {
        public TweetImageView imageView;
    }

    @Override
    public void getData(Object args) {
    }

    @Override
    protected void updateData(List<TweetSource> data) {
    }
}